﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace MadLibrary.Collections
{
    /// <summary>
    /// Provides generic enumerable and managable array.
    /// </summary>
    /// <typeparam name="T">Specifies reference parameter which must not have defined constructor.</typeparam>
    public class Array<T> : IEnumerable<T> where T : IComparable<T>
    {
        /// <summary>
        /// Holds data of current array.
        /// </summary>
        protected T[] data = new T[0];

        /// <summary>
        /// Gets count of elements in managable array.
        /// </summary>
        public int Count { get { return data.Length; } }

        /// <summary>
        /// Creates new empty managable array.
        /// </summary>
        public Array()
        {
        }

        /// <summary>
        /// Creates new managed array with specified items.
        /// </summary>
        /// <param name="data">Specifies items which have to be added into new managed array.</param>
        public Array(params T[] data)
        {
            this.data = data;
        }

        /// <summary>
        /// Creates new managed array with specified items.
        /// </summary>
        /// <param name="data">Specifies items which have to be added into new managed array.</param>
        public Array(Array<T> data)
        {
            this.data = data.ToArray();
        }

        /// <summary>
        /// Gets or sets item on specified position.
        /// </summary>
        /// <param name="i">Specifies index of item.</param>
        /// <returns>Element on specified position.</returns>
        public T this[int i]
        {
            get
            {
                return this.data[i];
            }
            set
            {
                this.data[i] = value;
            }
        }

        /// <summary>
        /// Adds specfied item to array.
        /// </summary>
        /// <param name="obj">Specifies item which has to be added.</param>
        public void Add(T obj)
        {
            int l = this.data.Length;
            T[] n = new T[l + 1];
            this.data.CopyTo(n, 0);
            n[l] = obj;
            this.data = n;
        }

        /// <summary>
        /// Adds multiply objects to array.
        /// </summary>
        /// <param name="objs">Specifies array of elements which has to be added.</param>
        public void Add(params T[] objs)
        {
            foreach(T o in objs)
            {
                this.Add(o);
            }
        }

        /// <summary>
        /// Inserts specified item at the specified position in array.
        /// </summary>
        /// <param name="position">The position.</param>
        /// <param name="item">The item.</param>
        public void InsertAt(int position, T item)
        {
            if(position < 0 || position > this.Count - 1)
                throw new IndexOutOfRangeException("Incorrect position passed for insert operation: Index was outside the array's size.");
            else if(position == 0)
            {
                T[] newarr = new T[this.data.Length + 1];
                newarr[0] = item;
                for(int i = 0; i < this.data.Length; i++)
                    newarr[i + 1] = this.data[i];
                this.data = newarr;
            }
            else if(position == this.data.Length - 1)
            {
                T[] newarr = new T[this.data.Length + 1];
                for(int i = 0; i < this.data.Length; i++)
                    newarr[i] = this.data[i];
                newarr[this.data.Length] = item;
                this.data = newarr;
            }
            else
            {
                T[] newarr = new T[this.data.Length + 1];
                for(int i = 0; i < position; i++)
                    newarr[i] = this.data[i];
                newarr[position] = item;
                for(int a = position + 1; a < this.data.Length; a++)
                    newarr[a] = this.data[a - 1];
            }
        }

        /// <summary>
        /// Gets indexes of item specified.
        /// </summary>
        /// <param name="obj">Specifies item whichs positions are searched.</param>
        /// <returns>Positions of specified item.</returns>
        public int[] IndexesOf(T obj)
        {
            int[] ret = new int[0];
            int i = 0;
            foreach(T d in this.data)
            {
                if(d.CompareTo(obj) == 0)
                {
                    int l = ret.Length;
                    int[] n = new int[l + 1];
                    ret.CopyTo(n, 0);
                    n[l] = i;
                }
                i++;
            }
            return ret;
        }

        /// <summary>
        /// Gets index of first occurence in array.
        /// </summary>
        /// <param name="obj">Specifies item whichs first occurence is searched.</param>
        /// <returns>First occurence of item specified.</returns>
        public int FirstIndexOf(T obj)
        {
            int i = 0;
            foreach(T o in this.data)
            {
                if(o.CompareTo(obj) == 0)
                {
                    return i;
                }
                i++;
            }
            return -1;
        }

        /// <summary>
        /// Gets index of last occurence in array.
        /// </summary>
        /// <param name="obj">Specifies item whichs last occurence is searched.</param>
        /// <returns>Last occurence of item specified.</returns>
        public int LastIndexOf(T obj)
        {
            int i = 0;
            int found = -1;
            foreach(T o in this.data)
            {
                if(o.CompareTo(obj) == 0)
                    found = i;
                i++;
            }
            return found;
        }

        /// <summary>
        /// Removes item specified.
        /// </summary>
        /// <param name="obj">Specifies item which has to be removed in current managable array.</param>
        /// <returns>Position of removed item.</returns>
        public int Remove(T obj)
        {
            int i = 0;
            int removed = -1;
            foreach(T o in this.data)
            {
                if(o.CompareTo(obj) == 0)
                {
                    removed = i;
                    break;
                }
            }
            var trmv = new List<T>(this.data);
            trmv.RemoveAt(removed);
            this.data = trmv.ToArray();
            return removed;
        }

        /// <summary>
        /// Removes item on specified position.
        /// </summary>
        /// <param name="pos">Specifies position of item which has to be removed.</param>
        public void RemoveAt(int pos)
        {
            var rm = new List<T>(this.data);
            rm.RemoveAt(pos);
            this.data = rm.ToArray();
        }

        /// <summary>
        /// Determines whether this managable array contains the specified item.
        /// </summary>
        /// <param name="item">The item.</param>
        /// <returns>
        ///   <c>true</c> if this array contains the specified item; otherwise, <c>false</c>.
        /// </returns>
        public bool Contains(T item)
        {
            foreach(T t in this.data)
            {
                if(t.CompareTo(item) == 0)
                    return true;
            }
            return false;
        }

        /// <summary>
        /// Clears current managable array.
        /// </summary>
        public void Clear()
        {
            this.data = new T[0];
        }

        /// <summary>
        /// Gets array of items from current managable array.
        /// </summary>
        /// <returns>Array of items of current managable array.</returns>
        public T[] ToArray()
        {
            return this.data;
        }

        /// <summary>
        /// Gets list from items of current array.
        /// </summary>
        /// <returns>List of items of current managable array.</returns>
        public List<T> ToList()
        {
            return this.data.ToList();
        }

        /// <summary>
        /// Clones current managable array.
        /// </summary>
        /// <returns>Copy of current managable array.</returns>
        public Array<T> Clone()
        {
            return new Array<T>(this.data);
        }

        /// <summary>
        /// Gets the enumerator.
        /// </summary>
        /// <returns>
        /// Enumerator for current managable array.
        /// </returns>
        IEnumerator<T> IEnumerable<T>.GetEnumerator()
        {
            return new ArrayEnumerator<T>(this);
        }

        /// <summary>
        /// Returns an enumerator that iterates through a collection.
        /// </summary>
        /// <returns>
        /// An <see cref="T:System.Collections.IEnumerator"/> object that can be used to iterate through the collection.
        /// </returns>
        System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
        {
            return new ArrayEnumerator<T>(this);
        }
    }

    /// <summary>
    /// Serves as enumerator for Array class.
    /// </summary>
    /// <typeparam name="T">The type.</typeparam>
    internal class ArrayEnumerator<T> : IEnumerator<T> where T : IComparable<T>
    {
        private Array<T> datac = new Array<T>();

        /// <summary>Initializes a new instance of the ArrayEnumerator class.</summary>
        /// <param name="data">The data.</param>
        public ArrayEnumerator(Array<T> data)
        {
            this.datac = data;
        }

        private int pos = -1;

        T IEnumerator<T>.Current
        {
            get { return this.datac[this.pos]; }
        }

        void IDisposable.Dispose()
        {
        }

        object System.Collections.IEnumerator.Current
        {
            get { return this.datac[this.pos]; }
        }

        bool System.Collections.IEnumerator.MoveNext()
        {
            if(pos < this.datac.Count - 1 && this.datac.Count != 0)
            {
                this.pos++;
                return true;
            }
            else return false;
        }

        void System.Collections.IEnumerator.Reset()
        {
            this.pos = -1;
        }
    }
}